<?php
namespace Admin\Controller\Blog;
use Admin\Controller\Auto\AutoController;
class ArticleController extends AutoController {
	public function index(){

		$this->display('/blog/article/index');
	}
	public function add(){
		$this->assign('list',TypeController::getIndex());
		$this->display('/blog/article/add');
	}
	public function insert(){
		// echo '<pre>';
		// var_dump(I('post.'));
		// var_dump($_FILES);exit;
		
		$m=M('b_article');
		$m->create($_POST);
		$m->pic=self::uploadPic('/upload/','./public/admin/blog');
		$m->uid=session('id');
		// $m->hot=0;
		$m->top=0;
		if($m->add()){
			echo '<script>alert("添加成功");window.location.href="/admin/blog/article/index"</script>';
		}else{
			echo '<script>alert("添加失败");window.location.href="/admin/blog/article/add"</script>';
		}
	}
	public function uptop(){
		// var_dump(I('post.id'));
		$m=M('b_article');
		$m->top=I('post.top');
		$res=$m->where('id='.I('post.id'))->save();
		if($res){
			echo 'yes';
		}else{
			echo 'no';
		}
	}
	public function upstatus(){
		// var_dump(I('post.'));
		$m=M('b_article');
		$m->status=I('post.status');
		$res=$m->where('id='.I('post.id'))->save();
		if($res){
			echo 'yes';
		}else{
			echo 'no';
		}
	}
	public function edit(){
		// echo $_GET['id'];
		$m=M('b_article');
		$res=$m->where('id='.I('get.id'))->find();
		// echo '<pre>';
		// var_dump($res);
		$this->assign('list',TypeController::getIndex());
		$this->assign('res',$res);
		$this->display('/blog/article/edit');
	}
	public function update(){
		// var_dump(I('post.'));
		// var_dump($_FILES['pic']['name']);
		$m=M('b_article');
		if($_FILES['pic']['name']!=''){
			$m->pic=self::uploadPic('/upload/','./public/admin/blog');
		}
		$m->uid=session('id');
		$m->tid=I('post.tid');
		$m->title=I('post.title');
		$m->label=I('post.label');
		$m->hot=I('post.hot');
		$m->top=I('post.top');
		$m->introduce=I('post.introduce');
		$m->status=I('post.status');
		$m->c_time=I('post.c_time');
		$m->content=$_POST['content'];
		$res=$m->where('id='.I('post.id'))->save();
		if($res){
			echo '<script>alert("文章修改成功");window.location.href="/admin/blog/article/index" </script>';
		}else{
			echo '<script>alert("文章修改失败");window.location.href="/admin/blog/article/index" </script>';
		}
	}
	public function del(){
		// var_dump(I('post.ids'));
		$aa=array();
		$m=M('b_article');
		$res=$m->select();
		foreach($res as $v){
			if(in_array($v['id'],I('post.ids'))){
				$m=M('b_article');
				$data=$m->where('id='.$v['id'])->delete();
				if($data){
					$aa[$v['id']]='yes';
				}else{
					$aa[$v['id']]='no';
				}
			}
		}
		if(in_array('no',$aa)){
			// echo 'no';
			$this->AjaxReturn(array_keys($aa,'no')); 
		}else{
			$this->AjaxReturn('yes'); 
		}
	}
	public static function uploadPic($savePath,$rootPath){
		$upload=new \Think\Upload();
		$upload->maxsize=3145728;
		$upload->exts=array('jpg','png','gif','jpeg');
		$upload->savePath=$savePath;
		$upload->rootPath=$rootPath;
		$info=$upload->upload();
		// if(!$info){
		// 	$this->error($upload->getError());
		// }
		return ltrim($upload->rootPath,'.').$info['pic']['savepath'].$info['pic']['savename'];
	}

	public function sel(){
		// var_dump(I('post.keyword'));
		$where['title|introduce']=array('like','%'.I('post.keyword').'%');
		// $where['introduce']=array('like','%'.I('post.keyword').'%');
		$m=M('b_article');
		$res=$m->alias('a')
		->field('a.*,b.type_name,c.username')
		->join('left join think_b_type b on a.tid=b.id')
		->join('left join think_user c on a.uid=c.id')
		->where($where)
		->limit(I('post.num'))
		->page(I('post.page'))
		->select();
		$res[count($res)]=ceil($m->where($where)->count()/I('post.num'));
		$this->ajaxReturn($res);
	}
	public function sendsitive(){
		$content = $_POST['content']; 
		$arr=array ('法轮大法', '退党',  '傻逼', '滚蛋', '去死', '他妈的', '胡锦涛', '江泽民', '习近平', '奥巴马', '朴槿惠');
		foreach ($arr as $v) { 
		    if (strstr($content, $v)){ 
		        echo ('no');
		    }else{
		    	echo ('yes');
		    }
		}
	}
	public function pinglun(){
		// var_dump(I('get.id'));exit;
		$m=M('b_pinglun');
		$res=$m->alias('p')
		->field('p.*,a.pic as apic,a.title,per.name,per.pic as perpic')
		->join('left join think_b_article a on a.id=p.aid')
		->join('left join think_personal per on per.uid=p.uid')
		->where('p.aid='.I('get.id'))
		->select();

		$n=M('b_reply');
		$data=$n->alias('re')
		->field('re.*,per.name,per.pic as perpic')
		->join('left join think_b_pinglun p on p.id=re.reid')
		->join('left join think_personal per on per.uid=re.uid')
		->select();

		$h=M('b_huifu');
		// $huifu=$h->select();
		$huifu=$h->alias('h')
		->field('h.*,per.name,per.pic,huifuper.name as huifuname')
		->join('left join think_personal per on per.uid=h.uid')
		->join('left join think_b_reply re on re.id=h.huifuid')
		->join('left join think_personal huifuper on huifuper.uid=re.uid')
		->select();

		$a=M('b_article');
		$art=$a->where('id='.I('get.id'))->find();
		// $this->assign('articleid',I('get.id'));
		$this->assign('art',$art);
		$this->assign('res',$res);
		$this->assign('data',$data);
		$this->assign('huifu',$huifu);
		$this->display('/blog/article/pinglun');
	}
	public function pl(){
		// var_dump(I('post.'));
		$m=M('b_pinglun');
		if($m->create()){
			$res=$m->add();
			if($res){
				echo 'alert("评论成功");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
			}else{
				echo 'alert("评论失败");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
			}
		}
	}
	public function delpl(){
		// var_dump(I('post.'));
		// $m=M('b_reply');
		// $res=$m->where('reid='.I('post.delid'))->select();
		// if($res){
		// 	echo 'no';
		// }else{
		// 	echo 'yes';
		// }
		if(I('post.aa')){
			// var_dump(I('post.aa'));
			$m=M('b_huifu');
			$res=$m->where('id='.I('post.aa'))->delete();
			if($res){
				echo 'alert("回复删除成功");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
			}else{
				echo 'alert("回复删除失败");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
			}
		}

		if(I('post.delpinglunid')){
			// echo 'delpinglun';
			$m=M('b_reply');
			$res=$m->where('reid='.I('post.delpinglunid'))->select();
			// $ress=[];
			// foreach($res as $v){
			// 	$ress[]=$v['id'];
			// }
			// var_dump($ress);
			// $ress=implode(',',$ress);
			// echo $ress;
			if($res){
				// echo 'alert("该评论有回复信息 是否一并删除？");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
				echo 'no';
			}else{
				// echo 'yes';
				$m=M('b_pinglun');
				$res=$m->where('id='.I('post.delpinglunid'))->delete();
				if($res){
					echo 'alert("回复删除成功");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
				}else{
					echo 'alert("回复删除失败");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
				}
			}
		}
		if(I('post.delpinglunidyes')){
			// echo '确定删除所有评论';
			$m=M('b_reply');
			$res=$m->where('reid='.I('post.delpinglunidyes'))->select();
			$ress=[];
			foreach($res as $v){
				$ress[]=$v['id'];
			}
			// var_dump($res);
			$h=M('b_huifu');
			$data=$h->select();
			// var_dump($data);
			foreach($data as $v){
				if(in_array($v["huifuid"],$ress)){
					// echo $v['id'];
					$res=$h->where('id='.$v['id'])->delete();
					if($res){
						$m=M('b_reply');
						$res=$m->where('reid='.I('post.delpinglunidyes'))->delete();
						if($res){
							$m=M('b_pinglun');
							$res=$m->where('id='.I('post.delpinglunidyes'))->delete();
							if($res){
								echo 'alert("回复删除成功");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
							}
						}
					}
				}else{
					$m=M('b_reply');
					$res=$m->where('reid='.I('post.delpinglunidyes'))->delete();
					if($res){
						$m=M('b_pinglun');
						$res=$m->where('id='.I('post.delpinglunidyes'))->delete();
						if($res){
							echo 'alert("回复删除成功");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
						}
					}
				}
			}
		}

		// if(I('post.replyyes')){
		// 	// echo I('post.replyyes');
		// 	$m=M('b_reply');
		// 	$res=$m->where('reid='.I('post.replyyes'))->delete();
		// 	if($res){
		// 		echo 'yes';
		// 	}else{
		// 		echo 'no';
		// 	}
		// }

		if(I('post.delreplyid')){
			// echo 'delreply';
			$m=M('b_huifu');
			$res=$m->where('huifuid='.I('post.delreplyid'))->select();
			// var_dump($res);
			if($res){
				echo 'no';
			}else{
				echo 'yes';
			}
		}

		if(I('post.delreplyidyes')){
			// var_dump(I('post.'));
			$m=M('b_huifu');
			$res=$m->where('huifuid='.I('post.delreplyidyes'))->delete();
			// if($res){
			// 	echo 'alert("回复删除成功");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
			// }else{
			// 	echo 'alert("回复删除失败");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
			// }
			if($res){
				$m=M('b_reply');
				$res=$m->where('id='.I('post.delreplyidyes'))->delete();
				if($res){
					echo 'alert("回复删除成功");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
				}else{
					echo 'alert("回复删除失败");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
				}
			}
		}
	}

	public function huifu(){
		// var_dump(I('post.'));
		if(I('post.reid')){
			// echo 'pinglun';
			$m=M('b_reply');
			if($m->create()){
				$res=$m->add();
				if($res){
					echo 'alert("回复成功");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
				}else{
					echo 'alert("回复失败");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
				}
			}
		}
		if(I('post.huifuid')){
			// echo 'reply';
			$m=M('b_huifu');
			if($m->create()){
				$res=$m->add();
				if($res){
					echo 'alert("回复成功");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
				}else{
					echo 'alert("回复失败");window.location.href="/admin/blog/article/pinglun?id='.I('post.aid').'"';
				}
			}
		}
	}
}
?>